﻿using NewRetail.Application.Components.Warehouse.Entity;
using NewRetail.Application.Core;
using System;
using System.Linq;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace NewRetail.Application.Components.Warehouse
{
    public class RateRulePagingDto : PagingRequestDto
    {
        
    }

    public interface IRateRuleService : IApplicationService
    {
        /// <summary>
        /// 附加仓库
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        Task<RateRule[]> AttachWarehouse(RateRule[] data);
    }

    public class RateRuleService : MyApplicationService, IRateRuleService
    {
        private readonly IRepository<RateRule> rateRules;

        public RateRuleService(IRepository<RateRule> rateRules)
        {
            this.rateRules = rateRules;
        }

        public async Task<RateRule[]> AttachWarehouse(RateRule[] data)
        {
            var db = await this.rateRules.GetDbContextAsync();

            await db.AttachData<Warehouse.Entity.Warehouse, RateRule>(data,
                idGetter: x => x.warehouseID,
                action: (m, arr) => m.Warehouse = arr.FirstOrDefault());

            return data;
        }
    }
}
